<template>
  <div>
    <a-card>
      <div style="text-align: left;">
        <span style="margin-right: 8px;color: rgba(0, 0, 0, 0.85);">企业:</span>
        <a-select v-model="enterpriseId" @change="enterpriseChange" showSearch style="min-width: 200px;">
          <a-select-option v-for="item in enterpriseData" :key="item.key">
            {{ item.name }}
          </a-select-option>
        </a-select>
      </div>
    </a-card>
  </div>
</template>

<script>
import store from '@/store'
import { mapGetters,mapActions } from 'vuex'
export default {
  name: 'enterpriseSelect',
  data() {
    return {
      enterpriseId: "",
      enterpriseData: [], // 客户列表
    }
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  activated() {
    this.getEnterpriseData('refresh');
  },
  methods: {
    // 获取企业列表
    async getEnterpriseData(type) {
      const data = await store.dispatch('department/getAllEnterprise');
      data.forEach(item => {
        item.key = item.name + ',' + item.enterpriseId
      })
      this.enterpriseData = data;
      if (type !== 'refresh') {
        this.enterpriseChange(this.enterpriseData[0].key);
      } else {
        this.enterpriseChange(this.enterpriseId);
      }
    },
    // 选择企业
    enterpriseChange(value) {
      this.enterpriseId = value;
      this.$emit('enterpriseChange',value.match(/,(\S*)/)[1],value.match(/(\S*),/)[1]);
    },
  }
}
</script>
